#!/bin/sh
# PCP QA Test No. 1362
# check postgresql PMDA can reconnect when db bounces
#
# Copyright (c) 2018 Red Hat.  All Rights Reserved.
#
# Note: this test is _notrun if a local postgresql server is not installed
# or not configured correctly and running. On Fedora (other platforms may
# differ), install, initialize, enable and start the server :
#
#     sudo dnf install postgresql-server
#     sudo postgresql-setup initdb
#     sudo systemctl enable postgresql
#     sudo systemctl start postgresql
#
# Also (on Fedora at least), after a major system upgrade may require you
# to migrate an existing postgres DB to the new version. To do that:
#
#     sudo dnf install postgresql-upgrade
#     sudo postgresql-setup --upgrade
#     sudo systemctl restart postgresql
#
# For Ubuntu, the major upgrade recipe is more like ...
#
#     # remove the on install empty main cluster
#     sudo pg_dropcluster --stop <newversion> main
#     sudo systemctl stop postgresql
#     sudo pg_upgradecluster -m upgrade <oldversion> main
#     sudo systemctl start postgresql
#     # check <oldversion> is down, <newversion> is online
#     pg_lsclusters
#     # remove the <oldversion> cluster
#     sudo pg_dropcluster --stop <oldversion> main
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -d $PCP_PMDAS_DIR/postgresql ] || _notrun "postgresql PMDA directory is not installed"
[ -f $PCP_PMDAS_DIR/postgresql/pmdapostgresql.python ] || _notrun "postgresql PMDA is not installed"

echo '\q' | $sudo -u postgres psql >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "Cannot run psql as the postgres user, postgresql not installed or running?"
$sudo -u postgres psql -c "select VERSION();" | grep -s "PostgreSQL 8" > /dev/null
[ $? -eq 0 ] && _notrun "not testing postgres v8.x, too old"

# need systemctl to reliably restart postgresql server
#
[ "$PCPQA_SYSTEMD" = yes ] || _notrun "systemctl not installed or not active"

CONF=$PCP_PMDAS_DIR/postgresql/pmdapostgresql.conf

status=1	# failure is the default!
trap "cd $here; $sudo mv $CONF.$seq $CONF; _cleanup_pmda postgresql; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

pmdapostgresql_remove()
{
    echo | tee -a $seq_full
    echo "=== remove postgresql agent ===" | tee -a $seq_full
    $sudo ./Remove >$tmp.out 2>>$seq_full
    cat $tmp.out >>$seq_full
    _filter_pmda_remove <$tmp.out
}

pmdapostgresql_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/postgresql
    $sudo mv $CONF $CONF.$seq
    cat <<EOF>$tmp.conf
[authentication]
host=local
port=5432
dbname=postgres
user=postgres
password=password
osuser=postgres
EOF

    $sudo mv $tmp.conf $CONF
    $sudo ./Remove >/dev/null 2>&1
    if ! _service pmcd stop 2>&1; then _exit 1; fi | _filter_pcp_stop

    echo | tee -a $seq_full
    echo "=== postgresql agent installation ===" | tee -a $seq_full
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out >>$seq_full
    _filter_pmda_install < $tmp.out | sed -e '1,/Updating the PMCD control file/d' \
    | $PCP_AWK_PROG '
/Check postgresql metrics have appeared/   { if ($7 < 20) $7 = "X"
                                          if ($9 >= 200) $9 = "Y"
                                          if ($12 >= 7000) $12 = "Z"
                                        }
                                        { print }'
}

_prepare_pmda postgresql

# note: _restore_auto_restart pmcd done in _cleanup_pmda()
_stop_auto_restart pmcd

# real QA test starts here
pmdapostgresql_install

echo;echo == check initial fetch
pminfo -f postgresql.stat.activity.usename >/dev/null 2>&1 || exit
echo OK

echo === restart postgresql service
$sudo systemctl restart postgresql >>$seq_full 2>&1

sleep 1

echo === checking log for disconnect messages
pminfo -f postgresql.stat.activity.usename >/dev/null 2>&1
grep -i "connection lost" $PCP_LOG_DIR/pmcd/postgresql.log | sed -e 's/^.*Info/Info/'

sleep 2

echo;echo == fetch again .. should have reconnected
pminfo -f postgresql.stat.activity.usename >/dev/null 2>&1 | exit
echo OK

echo "PMDA log file ..." >>$seq_full
$sudo cat $PCP_LOG_DIR/pmcd/postgresql.log >>$seq_full

pmdapostgresql_remove

status=0
exit
